package com.user.mediacoderdemo;

import android.media.MediaExtractor;
import android.media.MediaFormat;
import android.text.TextUtils;

import java.io.IOException;

/**
 * Created by zoudong on 2017/2/7.
 */

public class VideoInfoKit {

    public static MediaExtractor newIntance(String path)  {
        MediaExtractor mediaExtractor = new MediaExtractor();
        try {
            mediaExtractor.setDataSource(path);
        } catch (IOException e) {
            e.printStackTrace();
            return  null;
        }
        return mediaExtractor;
    }
    public static MediaFormat getMediaFormat(String tag,MediaExtractor extractor,boolean isselect){
        int trackCount = extractor.getTrackCount();
        for (int i = 0; i < trackCount; i++) {
            MediaFormat trackFormat = extractor.getTrackFormat(i);
            String mime = trackFormat.getString(MediaFormat.KEY_MIME);
            if (!TextUtils.isEmpty(mime)&&!TextUtils.isEmpty(tag)&&mime.startsWith(tag)) {
                if (isselect) {
                    extractor.selectTrack(i);
                }

                return trackFormat;
            }
        }
        return null;
    }
    public static MediaFormat getMediaFormat(String tag,MediaExtractor extractor){
       return  getMediaFormat(tag, extractor,true);
    }
}
